package com.titan.demo.camel.sample.eip.使用choice实现分支;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.LoggingLevel;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;

import com.titan.demo.camel.utils.RunCamel;

public class Test01 {
	public static void main(String[] args) {
        try {
        	RouteBuilder route = createRouteBuilder();
        	createRunCamel().run(route);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	private static RunCamel createRunCamel() {
		return new RunCamel() {
			protected void execute(CamelContext context,
					ProducerTemplate producer, ConsumerTemplate consumer)
					throws Exception {
				Map<String,Object> headers = new HashMap<String,Object>();
				headers.put("h1", "1111");
				headers.put("h2", "2222");
				headers.put("h3", "3333");
				
				producer.sendBodyAndHeaders("direct:in", "1", headers);
				producer.sendBodyAndHeaders("direct:in", "2", headers);
				producer.sendBodyAndHeaders("direct:in", "3", headers);
			}
		};
	}
	
	private static RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from("direct:in")
                .choice()
                    .when(body().isEqualTo("1"))
                    	.log(LoggingLevel.INFO, this.getClass().getName(), "Processing: ${in.header.h1}")
                    .when(body().isEqualTo("2"))
                    	.log(LoggingLevel.INFO, this.getClass().getName(), "Processing: ${in.header.h2}")
                    .otherwise()
                    	.log(LoggingLevel.INFO, this.getClass().getName(), "Processing: ${in.header.h3}")
                ;
            }
        };
    }
}
